package com.newegg.datafeed.adserving.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.newegg.datafeed.adserving.domain.Company;
import com.newegg.datafeed.adserving.domain.User;
import com.newegg.datafeed.adserving.domain.UserChannel;
import com.newegg.datafeed.adserving.mapper.CompanyMapper;
import com.newegg.datafeed.adserving.mapper.UserChannelMapper;
import com.newegg.datafeed.adserving.mapper.UserMapper;
import com.newegg.datafeed.adserving.request.UserForm;
import com.newegg.datafeed.adserving.response.Page;
import com.newegg.datafeed.adserving.response.UserInfo;

@Service
@Transactional
public class UserService {
	
	@Autowired
	UserMapper userMapper;
	
	@Autowired
	CompanyMapper companyMapper;
	
	@Autowired
	UserChannelMapper userChannelMapper;

	public Boolean checkExist(String username) {
		User user = userMapper.get(username);
		if(user == null){ return false; }
		return true;
	}

	public Page<User> pageByCompany(int company, Integer pageIndex, Integer pageSize) {
		if(pageIndex < 1){ pageIndex = 1;}
		Page<User> page = new Page<User>();
		page.setTotal(userMapper.count(company));
		page.setDatas(userMapper.page(company, (pageIndex - 1) * pageSize, pageSize));
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		page.getDatas().forEach(c->{ c.security(); });
		return page;
	}
	
	public UserInfo full(String username) {
		User user = userMapper.get(username);
		List<String> channel = userChannelMapper.getByUser(username);
		UserInfo info = new UserInfo();
		user.security();
		info.setUser(user);
		info.setChannel(channel);
		return info;
	}

	public User save(UserForm user) {
		if(checkExist(user.getUsername())){
			throw new RuntimeException("this account has exist");
		}
		Assert.notNull(user.getCompany(), "must have a company");
		Company company = companyMapper.get(user.getCompany());
		Assert.notNull(company, "must have a company");
		user.setCompanyName(company.getCompanyName());
		if(user.getDisable() == null){
			user.setDisable(false);
		}
		user.setCreateDate(new Date());
		user.setUpdateDate(new Date());
		userMapper.insert(user);
		user.getChannel().forEach(channel->{
			UserChannel uc = new UserChannel();
			uc.setChannel(channel);
			uc.setUser(user.getUsername());
			userChannelMapper.insert(uc);
		});
		user.security();
		return user;
	}

	public User update(UserForm user) {
		User old = userMapper.get(user.getUsername());
		Assert.notNull(old, "this account has not exist");
		if(user.getCompany() == null){
			user.setCompany(old.getCompany());
		}
		Company company = companyMapper.get(user.getCompany());
		Assert.notNull(company, "must have a company");
		user.setCompanyName(company.getCompanyName());
		if(user.getDisable() == null){
			user.setDisable(old.getDisable());
		}
		if(user.getPassword() == null){
			user.setPassword(old.getPassword());
		}
		user.setCreateDate(user.getCreateDate());
		user.setUpdateDate(new Date());
		userMapper.update(user);
		userChannelMapper.deleteByUser(user.getUsername());
		user.getChannel().forEach(channel->{
			UserChannel uc = new UserChannel();
			uc.setChannel(channel);
			uc.setUser(user.getUsername());
			userChannelMapper.insert(uc);
		});
		user.security();
		return user;
	}

	public void delete(String username) {
		userMapper.delete(username);
	}
}
